<?php

/**
* alltosun.com 商品属性控制器 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-8-11 上午11:01:22 $
* $Id$
*/

class Action
{
    private $per_page = 20;
    /**
     * 显示属性列表
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $is_advenced   = Request::getParam('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());

        // 拼装搜索条件
        $filter = array();
        if (isset($search_filter['title']) && $search_filter['title']) {
            $filter['`title` LIKE '] = '%'.$search_filter['title'].'%';
        }

        $filter['status'] = $status;

        $property_list = array();
        $property_count = _model('property')->getTotal($filter);
        if ($property_count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($property_count)) {
                Response::assign('pager', $pager);
            }

            $property_list = _model('property')->getList($filter, ' ORDER BY `id` DESC '.$pager->getLimit($page));
        }

        Response::assign('property_list', $property_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::assign('is_advenced', $is_advenced);

        Response::display('admin/property_list.html');
    }

    /**
     * 添加属性
     */
    public function add()
    {
        Response::display('admin/add.html');
    }

    /**
     * 编辑属性
     */
    public function edit()
    {
        $id = Request::getParam('id', 0);
        $property_info = _uri('property', $id);

        Response::assign('property_info', $property_info);
        Response::assign('id', $id);

        Response::display('admin/add.html');
    }

    /**
     * 保存
     */
    public function save()
    {
        $info = Request::getParam('info', array());
        $id   = Request::getParam('id', 0);

        $value_list = $info['value'];
        unset($info['value']);

        foreach ($info as $k=>$v) {
            if (!is_numeric($v)) {
                $info[$k] = trim(AnFilter::filter_string($v));
            }
        }

        foreach ($value_list as $k=>$v) {
            // 去掉空值
            if (!$v) {
                    continue;
                }
            if (!is_numeric($v)) {
                $value_list[$k] = trim(AnFilter::filter_string($v));
            }
            $new_value_list[$k] = $value_list[$k];
        }

        if (!isset($info['title']) || !$info['title']) {
            return '请填写属性标题';
        }

        if ($id) {
            $property_info = _uri('property', $id);
            if (!$property_info) {
                return '该内容不存在';
            }

            // 更新当前属性信息
            _model('property')->update($id, $info);

            //更新属性值信息
            $property_value_ids = _model('property_value')->getFields('id', array('property_id'=>$id));

            if (!empty($new_value_list)) {
                //删除已经被删除的属性
                $value_ids  = array_keys($new_value_list);
                $delete_ids = array_diff($property_value_ids, $value_ids);
                if ($delete_ids) {
                    _model('property_value')->delete(array('id'=>$delete_ids));
                }

                foreach ($new_value_list as $key=>$value) {
                    if (!$value) {
                        continue;
                    }
                    if (in_array($key, $property_value_ids)) {
                        _model('property_value')->update($key, array('value'=>$value));
                    } else {
                        _model('property_value')->create(array('property_id'=>$id, 'value'=>$value));
                    }
                }
            } else {
                _model('property_value')->delete(array('property_id'=>$id));
            }
        } else {
            $property_id = _model('property')->create($info);
            if ($property_id) {
                foreach ($new_value_list as $key=>$value) {
                    _model('property_value')->create(array('property_id'=>$property_id, 'value'=>$value));
                }
            }
        }

        Response::redirect(AnUrl('property/admin'));
    }

    /**
     * 删除
     * @return array
     */
    public function delete()
    {
        $id = Request::getParam('id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k=>$v) {
            if (!$v) {
                continue;
            }

            $property_info = _uri('property', (int)$v);
            if (!$property_info) {
                continue;
            }

            if (1 == $property_info['status']) {
                _model('property')->update($v, array('status'=>0));
            } else {
                _model('property')->delete($v);
            }
        }

        return 'ok';
    }

    /**
     * 更新状态
     * @return array
     */
    public function change_status()
    {
        $id     = Request::getParam('id', 0);
        $status = Request::getParam('status', 1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $property_info = _uri('property', $id);
        if ($status == $property_info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        if (1 == $status) {
            _model('property')->update($id, array('status'=>1));
        }

        return 'ok';
    }

    /**
     * ajax 通过property_id，查询value_list
     */
    public function check_value()
    {
        $property_id = Request::getParam('property_id', 0);

        $filter = array(
                'property_id' => $property_id
            );

        $value_list = _model('property_value')->getList($filter);
        if ($value_list) {
            return array('info'=>'ok', 'value_list'=>$value_list);
        }
        return 'error';
    }

    /**
     * ajax 查看属性值是否被使用
     */
    public function check_proerty_value()
    {
        $property_value_id = Request::getParam('property_value_id', 0);

        if (!$property_value_id) {
            return '请选择要删除的属性';
        }

        $have_total = _model('sku_relation')->getTotal(array('property_value_id'=>$property_value_id));

        if ($have_total > 0) {
            return 'ok';
        }

        return '未被使用';
    }
}
?>